package com.agile.leetcode.jvm.jit;

/**
 * PS E:\ownproject\leetcode> jmap -histo 17552
 * <p>
 * num     #instances         #bytes  class name
 * ----------------------------------------------
 * 1:           692       63110784  [I
 * 2:       1000000       16000000  com.agile.leetcode.jvm.jit.JitWithoutEscapeTest$User
 * 3:          4484        3655776  [B
 * 4:         13752        2192584  [C
 * 5:          8743         209832  java.lang.String
 * 6:           724          82992  java.lang.Class
 * <p>
 * jvm启动参数如下，关闭逃逸分析后：
 * -Xmx4G -Xms4G -XX:-DoEscapeAnalysis -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError
 * 启动后，使用jmap -histo 17552  17552 为当前jvm的进程id
 * 有1000000个实例JitWithoutEscapeTest$User分配在堆中，没有逃逸
 *
 * @author chenzhangkun
 * @ClassName JitWithoutEscapeTest
 * @description:
 * @date 2024年12月06日
 */
public class JitWithoutEscapeTest {
	public static void main(String[] args) {
		long a1 = System.currentTimeMillis();
		for (int i = 0; i < 1000000; i++) {
			alloc();
		}
		// 查看执行时间
		long a2 = System.currentTimeMillis();
		System.out.println("cost " + (a2 - a1) + " ms");
		// 为了方便查看堆内存中对象个数，线程sleep
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	}

	private static void alloc() {
		User user = new User();
	}

	static class User {

	}


}
